package com.boomsecret.demo;

import com.boomsecret.domain.OtherPerson;

import java.io.*;

/**
 * Created by jack on 2018/1/9.
 */
public class SerializeDemo {
//    public static void main(String[] args) throws Exception {
//        // Person已经实现序列化接口 Serializable
//        SupperPerson person = new SupperPerson();
//        person.setAge(18);
//        person.setUsername("tom");
//
//        File targetFile = new File("/Users/jack/Java/JavaDemo/temp.txt");
//
//        // 序列化person对象到temp.txt中
//        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(targetFile));
//        objectOutputStream.writeObject(person);
//        objectOutputStream.flush();
//        objectOutputStream.close();
//
//        // 反序列化person对象
//        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(targetFile));
//        SupperPerson newPerson = (SupperPerson) objectInputStream.readObject();
//        objectInputStream.close();
//        System.out.println(newPerson);
//    }

    public static void main(String[] args) throws Exception {
        // OtherPerson已经实现序列化接口Externalizable
        OtherPerson person = new OtherPerson();
        person.setAge(18);
        person.setUsername("tom");

        File targetFile = new File("/Users/jack/Java/JavaDemo/temp2.txt");

        // 序列化OtherPerson对象到temp2.txt中
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(targetFile));
        objectOutputStream.writeObject(person);
        objectOutputStream.flush();
        objectOutputStream.close();

        // 反序列化OtherPerson对象
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(targetFile));
        OtherPerson newPerson = (OtherPerson) objectInputStream.readObject();
        objectInputStream.close();
        System.out.println(newPerson);
    }
}
